<template>
  <el-container>
    <el-header>
      <!-- 头部区 -->
      <img src="../assets/images/logo.png" alt="" />
      <!-- 头部 右侧的菜单 -->
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        text-color="#fff"
        active-text-color="#409eff"
      >
        <!-- 下面的菜单没有下拉菜单，则使用 el-menu-item 标签 -->
        <!-- <el-menu-item>处理中心</el-menu-item> -->
        <!-- 下面的菜单有下拉菜单，则使用 el-submenu 标签 -->
        <el-submenu index="1">
          <template slot="title">
            <img alt="" class="avatar" :src="user.user_pic" v-if="user.user_pic" />
            <img class="avatar" src="../assets/logo.png" alt="" v-else/>
            个人中心
          </template>
          <el-menu-item index="1-1"><i class="el-icon-edit"></i>用户资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-edit"></i>重置密码</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-edit"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="logout"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px"
        ><div class="user-info">
          <img alt="" class="avatar" :src="user.user_pic" v-if="user.user_pic" />
          <img class="avatar" src="../assets/logo.png" alt="" v-else />
          <span>欢迎你{{ user.username || user.nickname }} </span>
        </div>
        <!-- 侧边栏的菜单 -->
        <el-menu
          background-color="#23262e"
          text-color="#fff"
          active-text-color="#409eff"
          default-active="$router.path"
          unique-opened
          class="aside-menu"
          router
        >
          <template v-for="item in menu">
            <el-menu-item
              :index="item.indexPath"
              v-if="item.children === null"
              :key="item.indexPath"
            >
              <i :class="item.icon"></i>{{ item.title }}
            </el-menu-item>
            <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
              <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
              <el-menu-item :index="v.indexPath" v-for="v in item.children" :key="v.indexPath">
                <i :class="v.icon"></i>{{ v.title }}
              </el-menu-item>
            </el-submenu>
          </template>

          <!-- <el-submenu index="bcd">
            <template slot="title"><i class="el-icon-s-custom"></i>个人中心</template>
            <el-menu-item index="ddd"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
            <el-menu-item index="eee"><i class="el-icon-key"></i>重置密码</el-menu-item>
            <el-menu-item index="fff"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          </el-submenu> -->
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 内容区, 放占位符,放其他组件 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/menu'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },
  methods: {
    // 退出的方法
    logout() {
      this.$confirm('确定要退出吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 点击了确定
          // console.log(111)
          // 确定退出，清除token、清除user
          this.$store.commit('user/updateToken', '')
          this.$store.commit('user/updateUser', {})
          this.$router.push('/login')
        })
        .catch(() => {})
    },
    // 获取菜单数据
    async getMenu() {
      // 调用API方法,获取数据,将数据存储到当前数据的数据项中
      const { data: res } = await getMenuAPI()
      // console.log(res.data)
      if (res.code === 0) {
        this.menu = res.data
      }
    }
  },
  created() {
    this.$store.dispatch('user/getUser')
    // 获取菜单的方法
    this.getMenu()
  },
  computed: {
    ...mapState('user', ['user'])
  }
}
</script>

<style lang="less" seoped>
.el-container {
  height: 100%;
}
.el-header {
  display: flex;
  background-color: #23262e;
  justify-content: space-between;
  align-items: center;
}
.el-aside {
  background-color: #23262e;
  .el-menu {
    width: 200px;
    overflow: hidden;
    .el-menu-item,
    el-submenu {
      font-size: 13px;
    }
  }
}
.el-main {
  background-color: #f2f2f2;
}
.avatar {
  width: 35px;
  height: 35px;
  background-color: #fff;
  border-radius: 50%;
  margin-right: 15px;
  object-fit: cover;
}
.user-info {
  height: 70px;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
}
</style>
